﻿using ET.DBCommon.DAL;
using ET.DBCommon.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ET.ScoreTeacherClient.Views
{
    /// <summary>
    /// EventLog.xaml 的交互逻辑
    /// </summary>
    public partial class EventLog : Window, INotifyPropertyChanged
    {
        private ObservableCollection<EventRecordModel> m_EventErrorList = new ObservableCollection<EventRecordModel>();

        public ObservableCollection<EventRecordModel> EventErrorList
        {
            get
            {
                return this.m_EventErrorList;
            }
            set
            {
                this.m_EventErrorList = value;
                this.OnPropertyChanged();
            }
        }

        private ObservableCollection<EventRecordModel> m_EventInfoList = new ObservableCollection<EventRecordModel>();

        public ObservableCollection<EventRecordModel> EventInfoList
        {
            get
            {
                return this.m_EventInfoList;
            }
            set
            {
                this.m_EventInfoList = value;
                this.OnPropertyChanged();
            }
        }

        private ObservableCollection<EventRecordModel> m_EventLogList = new ObservableCollection<EventRecordModel>();

        public ObservableCollection<EventRecordModel> EventLogList
        {
            get
            {
                return this.m_EventLogList;
            }
            set
            {
                this.m_EventLogList = value;
                this.OnPropertyChanged();
            }
        }

        private ObservableCollection<EventRecordModel> m_EventCurrentTaskList = new ObservableCollection<EventRecordModel>();

        public ObservableCollection<EventRecordModel> EventCurrentTaskList
        {
            get
            {
                return this.m_EventCurrentTaskList;
            }
            set
            {
                this.m_EventCurrentTaskList = value;
                this.OnPropertyChanged();
            }
        }

        #region INotifyPropertyChanged 接口实现
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName]string propName = null)
        {
            this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
        #endregion

        int ExamResultsID;
        bool HisMode;
        public EventLog()
        {
            InitializeComponent();
        }

        public EventLog(int m_ExamResultsID, bool m_HisMode)
        {
            InitializeComponent();
            this.ExamResultsID = m_ExamResultsID;
            this.HisMode = m_HisMode;
            this.DataContext = this;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.HisMode)
            {
                var records = ExamResultsDAL.GetEventRecords(this.ExamResultsID);
                this.EventInfoList = new ObservableCollection<EventRecordModel>(records.FindAll(p => p.EventLogType == LogType.info));
                this.EventErrorList = new ObservableCollection<EventRecordModel>(records.FindAll(p => p.EventLogType == LogType.error));
                this.EventLogList = new ObservableCollection<EventRecordModel>(records.FindAll(p => p.EventLogType == LogType.log));
                this.EventCurrentTaskList = new ObservableCollection<EventRecordModel>(records.FindAll(p => p.EventLogType == LogType.CurrentTask));
            }
            else
            {
                Task.Run(() =>
                {
                    while (true)
                    {
                        //var records = AccountServiceHelper.Instance.GetEventRecords();
                        //foreach (var log in records)
                        //{
                        //    Console.WriteLine(log.EventLogType.ToString() + "||" + log.Message + "||" + log.CreateTime);
                        //}
                        //if (records.Count > 0)
                        //    this.Dispatcher.Invoke(() =>
                        //    {
                        //        records.FindAll(p => p.EventLogType == LogType.info).ForEach(p => this.EventInfoList.Insert(0, p));
                        //        records.FindAll(p => p.EventLogType == LogType.error).ForEach(p => this.EventErrorList.Insert(0, p));
                        //        records.FindAll(p => p.EventLogType == LogType.log).ForEach(p => this.EventLogList.Insert(0, p));
                        //        records.FindAll(p => p.EventLogType == LogType.CurrentTask).ForEach(p => this.EventCurrentTaskList.Insert(0, p.Clone()));
                        //    });

                        System.Threading.Thread.Sleep(1000);
                    }
                });
            }
        }
    }
}
